package com.wuneng.ircontrol.myUtil.common;

import android.content.Context;

import com.wuneng.commonlibrary.util.SharedPreferencesHelper;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间类工具
 */
public class TimeUtils {
    /**
     * 判断两个时间之差是否大于range
     *
     * @param time
     * @param range
     * @return
     */
    public static boolean checkTime(String time, int range) {

        try {
            String serverTime = time;
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date(System.currentTimeMillis());
            Date date2 = df.parse(serverTime);

            long diff = date1.getTime() - date2.getTime();//这样得到的差值是微秒级别
            int day = (int) ((diff) / (1000 * 60 * 60 * 24));
            if (day > range) {
                return true;
            }
            return false;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 计算相差的秒数
     */
    public static boolean caculateSecond(String time1, String time2, long range) {
        long diff = 0;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = df.parse(time1);
            Date date2 = df.parse(time2);

            diff = date2.getTime() - date1.getTime();
            if (diff / 1000 > range)
                return true;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }


    /**
     * 计算推迟后的时间
     *
     * @param time
     * @return
     */
    public static String caculateTime(String time, int count) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(sdf.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.HOUR, count);
        Date d2 = cal.getTime();
        return sdf.format(d2);
    }


    /**
     * 计算入井时间
     *
     * @return
     */
    public static String getDatePoor(Context context) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(SharedPreferencesHelper.GetValue(context, "LoginTime", ""));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = System.currentTimeMillis() - date.getTime();
        // 计算差多少天
        // 计算差多少小时
        long hour = diff / nh;
        // 计算差多少分钟
        long min = diff % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nh % nm / ns;
        return (hour < 10 ? ("0" + hour) : hour) + ":" + (min < 10 ? ("0" + min) : min) + ":" + (sec < 10 ? ("0" + sec) : sec);
    }


}
